﻿package com.dusk.game
{
	import base.BaseHero;
	
	import com.dusk.util.SystemUtil;
	import com.game.data.constant.MaxAttribConst;
	import com.game.manager.GameSceneManager;
	import com.game.manager.NotificationManager;
	
	import flash.events.TimerEvent;
	import flash.system.fscommand;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	public final class HackChecker
	{
		
		private static const checkInterval:int = 10000;
		
		private static const blur:int = 500;
		public static var hackHandler:Function = defaultHackHandler;
		private static var prevDate:uint = 0;
		private static var prevTime:int = 0;
		
		public static function enabledCheckSpeedUp():void
		{
			var timer:Timer = new Timer(checkInterval);
			timer.addEventListener(TimerEvent.TIMER, timeHandler);
			timer.start();
		}
		
		private static function defaultHackHandler():void
		{
			NotificationManager.alert("请不要使用作弊工具!", 5, 30);
			if(SystemUtil.isApplication())
			{
				setTimeout(GameSceneManager.getIns().mainStage.nativeWindow.close, 5000);
			}
			else if(SystemUtil.isPlayer())
			{
				fscommand("quit");
			}
		}
		
		public static function checkAttribute(param1:BaseHero):Boolean
		{
			CONFIG::DEBUG
			{
				return false;
			}
			CONFIG::RELEASE
			{
				if (param1.roleProperties.hpMax > MaxAttribConst.MaxHp)
				{
					return true;
				}
				if (param1.roleProperties.mpMax > MaxAttribConst.MaxMp)
				{
					return true;
				}
				if (param1.roleProperties.atk > MaxAttribConst.MaxAtk)
				{
					return true;
				}
				if (param1.roleProperties.def > MaxAttribConst.MaxDef)
				{
					return true;
				}
				if (param1.roleProperties.crit > MaxAttribConst.MaxCrit)
				{
					return true;
				}
				if (param1.roleProperties.toughness > MaxAttribConst.MaxToughness)
				{
					return true;
				}
				if (param1.roleProperties.miss > MaxAttribConst.MaxMiss)
				{
					return true;
				}
				if (param1.roleProperties.hit > MaxAttribConst.MaxHit)
				{
					return true;
				}
				if (param1.roleProperties.magicDef > MaxAttribConst.MaxMagicDef)
				{
					return true;
				}
				if (param1.roleProperties.deMagicDef > MaxAttribConst.MaxReduceMagicDef)
				{
					return true;
				}
				if (param1.roleProperties.hpHeal > MaxAttribConst.MaxHx)
				{
					return true;
				}
				//noinspection RedundantIfStatementJS
				if (param1.roleProperties.mpHeal > MaxAttribConst.MaxHl)
				{
					return true;
				}
				return false;
			}
		}
		
		/**
		 * 检查AVM2运行时间和距离绝对时间间隔差距大小
		 * @param param1 计时器事件
		 */
		private static function timeHandler(param1:TimerEvent):void
		{
			if (prevDate > 0 && prevTime > 0)
			{
				var deltaDate:int = Math.abs(new Date().time - prevDate);
				var deltaTime:int = Math.abs(getTimer() - prevTime);
				if (Math.abs(deltaDate - deltaTime) > blur)
				{
					hackHandler();
				}
			}
			prevDate = new Date().time;
			prevTime = getTimer();
		}
	}
}
